from PyQt4.QtCore import *
from PyQt4.QtGui import *

from ops import InputItem, CancelException
from ops import StringT, NumberT
import properties

import Raster

from simpleRaster import Ui_SimpleRaster
class SimpleRaster(InputItem):
    name = "SimpleRaster"
    def __init__(self,x,y):
        InputItem.__init__(self,x,y,[],["Out"]," Simple Raster")
        self.propsDialog = Ui_SimpleRaster
        self.setToolTip("A simple raster")
        self.outputs[0].value = None
        self.props = {'nameString': "Simple Raster",  'columns': 1, 'rows': 1, "xleft": 0, 
                      "ytop":0,"cellHeight":1,"cellWidth":1,
                      "startValue":0,"incrementValue":0}
        if not self.properties():
            raise CancelException
    def displayText(self):
        if not hasattr(self,"props"):
            return "Simple raster"
        return self.props['nameString']
    def propDef(self):
        return {'nameString': StringT,  'columns': NumberT, 'rows': NumberT,
                "xleft": NumberT, "ytop":NumberT,
                "cellHeight":NumberT,"cellWidth":NumberT,
                "startValue":NumberT,"incrementValue":NumberT}
    def vnames(self):
        return ["nameString", "rows","columns","xleft","ytop","cellHeight","cellWidth","startValue","incrementValue"]

    def properties(self):
        d = properties.dialog(self.propsDialog())
        # set dialog from object's values
        self.setDialogValues(d.ui)
        if properties.show(d):
            command = CommandEditSimpleRaster(self,self.getDialogValues(d.ui))
            if self.scene():
                self.scene().undoStack.push(command)
            else:
                command.redo()
            return True
        else:
            return False
        
    def process(self):
        self.outputs[0].value=Raster.fromSimpleRaster(self.props)
        pass

class CommandEditSimpleRaster(QUndoCommand):
    def __init__(self,item,newValues):
        QUndoCommand.__init__(self,"Edit simple raster")
        self.newValues = newValues
        self.item = item
        self.oldValues = item.props

    def redo(self):
        item = self.item
        item.props = self.newValues
        #item.outputs[0].value = self.newValues
        self.update()

    def undo(self):
        item = self.item
        item.props = self.oldValues
        self.update()

    def update(self):
        item = self.item
        item.setPlainText(item.displayText())
        item.locateOutputHandles()
        item.update(item.boundingRect())
